using System.Text;
using UnityEditor;
using UnityEngine;

public class ExportWndField
{
    [MenuItem("Assets/Mini Tools/UI/Copy UIWidget", false, 1)]
    [MenuItem("GameObject/Mini Tools/GameObject/Copy UIWidget", false, 1)]
    private static void Main()
    {
        var go = Selection.activeGameObject;
        if (go == null)
        {
            return;
        }
        StringBuilder builder = new();

        string path = GetPath(go.transform);

        builder.AppendLine($"\t\t[UIField(\"{path}\")]");
        builder.AppendLine($"\t\tprivate readonly Widget {go.name};");

        EditorGUIUtility.systemCopyBuffer = builder.ToString();

        Debug.Log("Copy UIWidget Complete");
    }

    private static string GetPath(Transform trans)
    {
        if (trans == null)
        {
            return string.Empty;
        }
        string path = trans.name;
        while (trans.parent != null)
        {
            trans = trans.parent;
            var n = trans.name.ToLower();
            if (n.EndsWith("wnd") || n.StartsWith("view_"))// || n.EndsWith("view") || n.StartsWith("view")
            {
                break;
            }
            path = $"{trans.name}/{path}";
        }

        return path;
    }
}
